﻿#include "basenode.h"

#include <osg/Drawable>
#include <osg/LineWidth>
#include <osg/ShapeDrawable>
#include <osgText/Font>
#include <osgText/Text>
#include <osg/LineStipple>

#define YELLOW          osg::Vec4(1,1,0,1)
#define BLUE            osg::Vec4(0,0,1,TRANSPARENCY)
#define SKYBLUE         osg::Vec4(0,1,1,TRANSPARENCY)
#define PINK            osg::Vec4(1,0,1,TRANSPARENCY)
#define VIOLET          osg::Vec4(0.375,0,1,TRANSPARENCY)
#define LIGHTBLUE       osg::Vec4(0,0.375,1,TRANSPARENCY)
#define BLUEVIOLET      osg::Vec4(0.563,0,1,TRANSPARENCY)
#define ORANGE          osg::Vec4(1,0.375,0,TRANSPARENCY*1.2)
#define YELLOWGREEN     osg::Vec4(0.81,1,0,TRANSPARENCY*1.2)
#define GRAY            osg::Vec4(0.8,0.8,0.8,TRANSPARENCY)

namespace HIM {
BaseNode::BaseNode():
    baseNode_(new osg::Geode)
{    
    initBaseNode();
}

void BaseNode::initBaseNode()
{
    //创建保存几何信息的对象
    osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();

    //创建顶点
    osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array();
    v->push_back(osg::Vec3(-150.0f , 150.0f , 0.0f));
    v->push_back(osg::Vec3(150.0f, 150.0f, 0.0f));

    v->push_back(osg::Vec3(-150.0f , 120.0f , 0.0f));
    v->push_back(osg::Vec3(150.0f, 120.0f, 0.0f));

    v->push_back(osg::Vec3(-150.0f , 90.0f , 0.0f));
    v->push_back(osg::Vec3(150.0f, 90.0f, 0.0f));

    v->push_back(osg::Vec3(-150.0f , 60.0f , 0.0f));
    v->push_back(osg::Vec3(150.0f, 60.0f, 0.0f));

    v->push_back(osg::Vec3(-150.0f , 30.0f , 0.0f));
    v->push_back(osg::Vec3(150.0f, 30.0f, 0.0f));

    v->push_back(osg::Vec3(-150.0f , 0.0f , 0.0f));
    v->push_back(osg::Vec3(150.0f, 0.0f, 0.0f));

    v->push_back(osg::Vec3(-150.0f , -150.0f , 0.0f));
    v->push_back(osg::Vec3(150.0f, -150.0f, 0.0f));

    v->push_back(osg::Vec3(-150.0f , -120.0f , 0.0f));
    v->push_back(osg::Vec3(150.0f, -120.0f, 0.0f));

    v->push_back(osg::Vec3(-150.0f , -90.0f , 0.0f));
    v->push_back(osg::Vec3(150.0f, -90.0f, 0.0f));

    v->push_back(osg::Vec3(-150.0f , -60.0f , 0.0f));
    v->push_back(osg::Vec3(150.0f, -60.0f, 0.0f));

    v->push_back(osg::Vec3(-150.0f , -30.0f , 0.0f));
    v->push_back(osg::Vec3(150.0f, -30.0f, 0.0f));

    v->push_back(osg::Vec3(-150.0f , 150.0f , 0.0f));
    v->push_back(osg::Vec3(-150.0f, -150.0f, 0.0f));

    v->push_back(osg::Vec3(-120.0f , 150.0f , 0.0f));
    v->push_back(osg::Vec3(-120.0f, -150.0f, 0.0f));

    v->push_back(osg::Vec3(-90.0f , 150.0f , 0.0f));
    v->push_back(osg::Vec3(-90.0f, -150.0f, 0.0f));

    v->push_back(osg::Vec3(-60.0f , 150.0f , 0.0f));
    v->push_back(osg::Vec3(-60.0f, -150.0f, 0.0f));

    v->push_back(osg::Vec3(-30.0f , 150.0f , 0.0f));
    v->push_back(osg::Vec3(-30.0f, -150.0f, 0.0f));

    v->push_back(osg::Vec3(0.0f , 150.0f , 0.0f));
    v->push_back(osg::Vec3(0.0f, -150.0f, 0.0f));

    v->push_back(osg::Vec3(150.0f , 150.0f , 0.0f));
    v->push_back(osg::Vec3(150.0f, -150.0f, 0.0f));

    v->push_back(osg::Vec3(120.0f , 150.0f , 0.0f));
    v->push_back(osg::Vec3(120.0f, -150.0f, 0.0f));

    v->push_back(osg::Vec3(90.0f , 150.0f , 0.0f));
    v->push_back(osg::Vec3(90.0f, -150.0f, 0.0f));

    v->push_back(osg::Vec3(60.0f , 150.0f , 0.0f));
    v->push_back(osg::Vec3(60.0f, -150.0f, 0.0f));

    v->push_back(osg::Vec3(30.0f , 150.0f , 0.0f));
    v->push_back(osg::Vec3(30.0f, -150.0f, 0.0f));

    geom->setVertexArray(v.get());

    osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array();
    c->push_back(osg::Vec4(0.5f, 0.5f, 0.5f, 1.0f));
    c->push_back(osg::Vec4(0.5f, 0.5f, 0.5f, 1.0f));
    geom->setColorArray(c.get());
    geom->setColorBinding(osg::Geometry::BIND_OVERALL);
    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,44));

    //虚线
    osg::ref_ptr<osg::LineStipple> lineStipple = new osg::LineStipple();
    lineStipple->setFactor(1);
    lineStipple->setPattern(0xAFFF);
    baseNode_->getOrCreateStateSet()->setAttributeAndModes(lineStipple, osg::StateAttribute::ON);

    osg::ref_ptr<osgText::Text> pText0 = new osgText::Text;
    pText0->setText("0");
    pText0->setFont("Fonts/simhei.ttf");
    pText0->setColor(osg::Vec4(0,0,0,1));
    pText0->setAxisAlignment(osgText::Text::SCREEN);
    pText0->setCharacterSize(8);
    pText0->setPosition(osg::Vec3(-158.f, -158.f, 0.0f));

    osg::ref_ptr<osgText::Text> pTextX1 = new osgText::Text;
    pTextX1->setText("150");
    pTextX1->setFont("Fonts/simhei.ttf");
    pTextX1->setColor(osg::Vec4(0,0,0,1));
    pTextX1->setAxisAlignment(osgText::Text::SCREEN);
    pTextX1->setCharacterSize(8);
    pTextX1->setPosition(osg::Vec3(-6.f, -158.f, 0.0f));

    osg::ref_ptr<osgText::Text> pTextX2 = new osgText::Text;
    pTextX2->setText("300");
    pTextX2->setFont("Fonts/simhei.ttf");
    pTextX2->setColor(osg::Vec4(0,0,0,1));
    pTextX2->setAxisAlignment(osgText::Text::SCREEN);
    pTextX2->setCharacterSize(8);
    pTextX2->setPosition(osg::Vec3(144.f, -158.f, 0.0f));


    osg::ref_ptr<osgText::Text> pTextY1 = new osgText::Text;
    pTextY1->setText("150");
    pTextY1->setFont("Fonts/simhei.ttf");
    pTextY1->setColor(osg::Vec4(0,0,0,1));
    pTextY1->setAxisAlignment(osgText::Text::SCREEN);
    pTextY1->setCharacterSize(8);
    pTextY1->setPosition(osg::Vec3(-164.f, -3.f, 0.0f));

    osg::ref_ptr<osgText::Text> pTextY2 = new osgText::Text;
    pTextY2->setText("300");
    pTextY2->setFont("Fonts/simhei.ttf");
    pTextY2->setColor(osg::Vec4(0,0,0,1));
    pTextY2->setAxisAlignment(osgText::Text::SCREEN);
    pTextY2->setCharacterSize(8);
    pTextY2->setPosition(osg::Vec3(-164.f, 147.f, 0.0f));

    //baseNode_->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
    baseNode_->getOrCreateStateSet()->setAttribute(new osg::LineWidth(1), osg::StateAttribute::ON);

    baseNode_->addDrawable(geom.get());
    baseNode_->addDrawable(pText0.get());
    baseNode_->addDrawable(pTextX1.get());
    baseNode_->addDrawable(pTextX2.get());
    baseNode_->addDrawable(pTextY1.get());
    baseNode_->addDrawable(pTextY2.get());
}

}
